"""
上下文语法
"""
import sys

from contextlib import contextmanager
from time import time

sys.setrecursionlimit(5000)


@contextmanager
def record_time():
    start = time()
    yield 'hello'
    end = time()
    print(f'执行时间: {end - start}秒')


class RecordTime:

    def __init__(self):
        self.start = 0
        self.value = 'hello'

    def __enter__(self):
        self.start = time()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        end = time()
        print(f'执行时间: {end - self.start}秒')


def fac(num):
    if num in (0, 1):
        return 1
    return num * fac(num - 1)


with RecordTime() as rt:
    print(rt.value)
    num = fac(12)
print(num)
